package com.antexpress.service;

import com.antexpress.bean.Page;
import com.antexpress.common.Const;
import com.antexpress.common.Response;
import com.antexpress.dao.AreaDAO;
import com.antexpress.dao.CourierDAO;
import com.antexpress.dao.DepartmentDAO;
import com.antexpress.dao.EmployeeDAO;
import com.antexpress.pojo.Employee;
import com.antexpress.pojo.ExpressDistribution;
import com.antexpress.pojo.MapTown;
import com.antexpress.pojo.Title;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourierServiceImp implements CourierService{
    @Autowired
    private CourierDAO courDAO;
    @Autowired
    private EmployeeDAO empDAO;
    @Autowired
    private DepartmentDAO depDAO;
    @Autowired
    private AreaDAO areaDAO;
    
    @Override
    public Response courierShowOnPage(String province,String city,String district,String town,int currentPage, int pageSize){
        Response resp = null;
        Page page = new Page();
        int allRow = 0;
        int offset = page.countOffset(currentPage,pageSize);
        List<ExpressDistribution> courList = null;
        if(!town.isEmpty()){
            courList = new ArrayList<ExpressDistribution>();
            courList.add(courDAO.getCourierByTownId(town));
        }
        else if(!district.isEmpty()){
            courList = courDAO.getCourierForPageByDistrict(district, offset, pageSize);
        }
        else if(!city.isEmpty()){
            courList = courDAO.getCourierForPageByCity(city, offset, pageSize);
        }
        else if(!province.isEmpty()){
            courList = courDAO.getCourierForPageByProvince(province, offset, pageSize);
        }
        else{
            courList = courDAO.getCourierForPage(offset, pageSize);
        }
        allRow = courDAO.getCountForPage();
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(courList);
        resp = Response.createBySuccess(page);
        return resp;
    }
    
    @Override
    public Response getCourierList(){
        Response resp = null;
        Title title = depDAO.getTitleById(Const.COURIER_ID);
        List<Employee> courList = empDAO.getInServiceEmpsByTitle(title);
        resp = Response.createBySuccess(courList);
        return resp;
    }

    @Override
    public Response changeCourier(String townId, String courierId) {
        Response resp = null;
        MapTown town = areaDAO.getTownById(townId);
        Employee emp = empDAO.getEmployeeById(courierId);
        boolean isSuccess = courDAO.changeCourier(town, emp);
        if(isSuccess == true)
            resp = Response.createBySuccess();
        else
            resp = Response.createByErrorMessage("Update distribution infomation failed!");
        return resp;
    }

    @Override
    public Response deleteCourier(String id) {
        Response resp = null;
        MapTown town = areaDAO.getTownById(id);
        boolean isSuccess = courDAO.deleteCourier(town);
        if(isSuccess == true)
            resp = Response.createBySuccess();
        else
            resp = Response.createByErrorMessage("Update distribution infomation failed!");
        return resp;
    }
    
    
}
